let User = require('../model/user')
let Works = require('../model/work')
let result = require('../utils/result')
let format = require('../utils/format.js')
function WorkController(){

};

/**
 * 查询所有用户
 * @param req
 * @param res
 * @returns {Promise<void>}
 */
WorkController.prototype.getWork = async (req,res)=>{
    let uid = req.body.uid;
    if(!uid){
        res.json(result(-1,'请求参数不合法'))
        return;
    }
    let works = await Works.findAll({where: {uid:uid,isDel: 0}});
    res.status(200).json(result(0,'success',works))
}

WorkController.prototype.createWorks = async (req,res)=>{
    let uid = req.body.uid;
    let imgurl = req.body.imgurl;
    let remark = req.body.remark;
    if(!uid){
        res.json(result(-1,'请求参数不合法'))
        return;
    };
    if(!imgurl){
        res.json(result(-1,'请求参数不合法'))
        return;
    }
    Works.create({imgurl: imgurl,uid:uid}).then(rsp=>{
        res.json(result(0,'success'))
    }).catch(()=>{
        res.json(result(-1,'fail'))
    })
}

WorkController.prototype.delWorks = async (req,res)=>{
    let uid = req.body.uid;
    let id = req.body.id;
    if(!uid){
        res.json(result(-1,'请求参数不合法'))
        return;
    };
    if(!id){
        res.json(result(-1,'请求参数不合法'))
        return;
    }
    Works.update({isDel:1},{where:{uid:uid,id:id}}).then(rsp=>{
        res.json(result(0,'success'))
    }).catch(()=>{
        res.json(result(-1,'fail'))
    })
}




let workController = new WorkController();
module.exports = workController
